package com.wanmait.cookbook.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.wanmait.cookbook.dao.CookbookCollectDAO;
import com.wanmait.cookbook.dao.CookbookCommentDAO;
import com.wanmait.cookbook.dao.CookbookDAO;
import com.wanmait.cookbook.dao.IngredientsConsumptionDAO;
import com.wanmait.cookbook.dao.ProvinceDAO;
import com.wanmait.cookbook.dao.RecipeTypeDAO;
import com.wanmait.cookbook.dao.StepDAO;
import com.wanmait.cookbook.vo.Cookbook;
import com.wanmait.cookbook.vo.CookbookComment;
import com.wanmait.cookbook.vo.IngredientsConsumption;
import com.wanmait.cookbook.vo.RecipeType;
import com.wanmait.cookbook.vo.RecipeTypeSecond;
import com.wanmait.cookbook.vo.Step;
import com.wanmait.cookbook.vo.Userinfo;


/**
 * Servlet implementation class UpCookbook
 */
@WebServlet("/CookbookServlet")
@MultipartConfig()
public class CookbookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CookbookServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String action = request.getParameter("action");
		if ("init".equals(action)) {
			this.init(request, response);
		}else if ("upCookbook".equals(action)) {
			this.upCookbook(request, response);
		}else if ("cookbookShow".equals(action)) {
			this.cookbookShow(request, response);
		}else if ("draftsReleaseDraft".equals(action)) {
		      this.draftsReleaseDraft(request, response);
	    }else if ("draftsSaveDraft".equals(action)) {
	        this.draftsSaveDraft(request, response);
	    }else if ("showDraftDetails".equals(action)) {
	        this.showDraftDetails(request, response);
	    }else if ("savedraft".equals(action)) {
	        this.savedraft(request, response);
	    } else if ("showDraft".equals(action)) {
	      this.showDraft(request, response);
	    }else if("addPageviews".equals(action)) {
	    	this.addPageviews(request, response);
	    }else if ("judgeCookBookName".equals(action)) {
			this.judgeCookBookName(request, response);
		}else if ("draftBoxRemoveCookbook".equals(action)) {
			this.draftBoxRemoveCookbook(request, response);
		}

	}
	
	protected void draftBoxRemoveCookbook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cookBookId = request.getParameter("cookBookId");
		CookbookDAO.getCookbookDAO().removeCookbook(Integer.parseInt(cookBookId));
	}
	
	/**
	 *  判断食谱名
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void judgeCookBookName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cookBookName = request.getParameter("cookBookName");
		Boolean flag = CookbookDAO.getCookbookDAO().judgeCookBookName(cookBookName);
		response.getWriter().print(flag);
	}
	
	
	//增加浏览量
	protected void addPageviews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cookbookId=request.getParameter("cookbookId");
		CookbookDAO.getCookbookDAO().addPageviews(Integer.parseInt(cookbookId));
		
	
	
	
	}
		
		
		/**
	   *  显示草稿
	   * @param request
	   * @param response
	   * @throws ServletException
	   * @throws IOException
	   */
	  protected void showDraft(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    Userinfo userinfo = (Userinfo)request.getSession().getAttribute("userinfo");
	    List<Cookbook> draftCookbook = CookbookDAO.getCookbookDAO().getDraftCookbook(userinfo.getId());
	    request.setAttribute("draftCookbook", draftCookbook);
	    request.getRequestDispatcher("/usercenter/draftBox.jsp").forward(request, response);
	  }

	
	/**
	   *  保存草稿
	   * @param request
	   * @param response
	   * @throws ServletException
	   * @throws IOException
	   */
	  protected void savedraft(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    Userinfo userinfo = (Userinfo)request.getSession().getAttribute("userinfo");
	    //Userinfo userinfo = new Userinfo();
	    //userinfo.setId(1);
	    Collection<Part> parts = request.getParts();
	    String filePath = request.getServletContext().getRealPath("/static/image/");
	    String pic = null; //主图
	    String video = null; // 视频
	    List<String> foodPICs = new ArrayList<String>();
	    for (Part part : parts) {
	      // 视频
	      if ("foodVidoe".equals(part.getName())) {
	        String head = part.getHeader("content-disposition");
	        int i = head.lastIndexOf(".");
	        String fileext = head.substring(i, head.length()-1);
	        String filename = UUID.randomUUID().toString();
	       
	        video = filename+fileext;
	        part.write(filePath+"/"+filename+fileext);
	        part.delete();
	      }
	      // 封面图
	      if ("foodPICCover".equals(part.getName())) {
	        String head = part.getHeader("content-disposition");
	        int i = head.lastIndexOf(".");
	        String fileext = head.substring(i, head.length()-1);
	        String filename = UUID.randomUUID().toString();
	        pic = filename+fileext;
	        part.write(filePath+"/"+filename+fileext);
	        part.delete();
	      }
	      // 步骤图
	      if ("foodPIC".equals(part.getName())) {
	        String head = part.getHeader("content-disposition");
	        int i = head.lastIndexOf(".");
	        String fileext = head.substring(i, head.length()-1);
	        String filename = UUID.randomUUID().toString();
	        foodPICs.add(filename+fileext);
	        part.write(filePath+"/"+filename+fileext);
	        part.delete();
	      }
	    }
	    String title = request.getParameter("cook_name"); // 食谱标题
	    Integer cookingDifficulty = Integer.parseInt(request.getParameter("cook_difficulty")); // 烹饪难度
	    Integer cookingTime = Integer.parseInt(request.getParameter("costtime")); // 烹饪时间
	    String taste = request.getParameter("taste"); //口味
	    String cookingMethod = request.getParameter("cooking_method"); // 烹饪方式
	    String recipeTypeSecondId = request.getParameter("recipeTypeSecond"); // 食谱二级类型id
	    String info = request.getParameter("cookStory"); // 简介
	    String tips = request.getParameter("cooktips"); // 小贴士
	    Cookbook cookbook = new Cookbook();
	    cookbook.setTitle(title);
	    cookbook.setPic(pic);
	    cookbook.setUserinfo(userinfo);
	    cookbook.setInfo(info);
	    cookbook.setTips(tips);
	    RecipeTypeSecond recipeTypeSecond = new RecipeTypeSecond();
	    recipeTypeSecond.setId(Integer.parseInt(recipeTypeSecondId));
	    cookbook.setRecipeTypeSecond(recipeTypeSecond);
	    cookbook.setCookingDifficulty(cookingDifficulty);
	    cookbook.setCookingTime(cookingTime);
	    cookbook.setTaste(taste);
	    cookbook.setCookingMethod(cookingMethod);
	    cookbook.setVideo(video);
	    cookbook.setDraftBox(true);
	    
	    CookbookDAO.getCookbookDAO().addCookbook(cookbook); // 上传菜谱

	    // 添加步骤
	    Step step = null;
	    String[] setpInfos = request.getParameterValues("setpInfos"); // 步骤简介
	    for (int i = 0; i < setpInfos.length; i++) {
	      step = new Step();
	      step.setPic(foodPICs.get(i));
	      step.setInfo(setpInfos[i]);
	      StepDAO.getStepDAO().addStep(step); // 添加步骤
	    }
	    
	    // 添加食材
	    String[] zhuliao = request.getParameterValues("zhuliao"); // 食材清单
	    String[] zhuliaoValue = request.getParameterValues("zhuliaoValue"); // 食材用量
	    IngredientsConsumption ingredientsConsumption = null;
	    for (int i = 0; i < zhuliao.length; i++) {
	      ingredientsConsumption = new IngredientsConsumption();
	      ingredientsConsumption.setIngredientsName(zhuliao[i]);
	      ingredientsConsumption.setDosage(zhuliaoValue[i]);
	      IngredientsConsumptionDAO.getIngredientsConsumptionDAO().addIngredients(ingredientsConsumption); // 添加食材
	    }
	    request.getRequestDispatcher("/CookbookServlet?action=showDraft").forward(request, response);
	  }
	
	  /**
	   *  显示草稿详情
	   * @param request
	   * @param response
	   * @throws ServletException
	   * @throws IOException
	   */
	  protected void showDraftDetails(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	     Userinfo userinfo = (Userinfo)request.getSession().getAttribute("userinfo");
//	    Userinfo userinfo = new Userinfo();
//	    userinfo.setId(1);
	    String cookbookId = request.getParameter("cookbookId");
	    Cookbook draftCookbookDetails = CookbookDAO.getCookbookDAO().getDraftCookbookDetails(Integer.parseInt(cookbookId));
	    List<RecipeType> findByRecipeType = RecipeTypeDAO.getRecipeTypeDAO().findByRecipeType();
	    List<Step> findStepsByCookbookId = StepDAO.getStepDAO().findStepsByCookbookId(Integer.parseInt(cookbookId));
	    List<IngredientsConsumption> findConsumptionByCookbookId = IngredientsConsumptionDAO.getIngredientsConsumptionDAO().findConsumptionByCookbookId(Integer.parseInt(cookbookId));
	    request.setAttribute("draftCookbookDetails", draftCookbookDetails);
	    request.setAttribute("findByRecipeType", findByRecipeType);
	    request.setAttribute("findStepsByCookbookId", findStepsByCookbookId);
	    request.setAttribute("findConsumptionByCookbookId", findConsumptionByCookbookId);
	    request.getRequestDispatcher("/draftCookbookShow.jsp").forward(request, response);
	  }
	
	/**

	 *  通过草稿箱保存草稿

	 * @param request

	 * @param response

	 * @throws ServletException

	 * @throws IOException

	 */

	protected void draftsSaveDraft(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		Userinfo userinfo = (Userinfo)request.getSession().getAttribute("userinfo");

		String cookbookId = request.getParameter("cookbookId");

		Collection<Part> parts = request.getParts();

		String filePath = request.getServletContext().getRealPath("/static/image/");

		String pic = null; //主图

		String video = null; // 视频

		List<String> foodPICStep = new ArrayList<String>(); // 修改的步骤图

		List<String> foodPIC = new ArrayList<String>();// 新添加的步骤图

		String[] StepPIC = request.getParameterValues("StepPIC");

		for (Part part : parts) {

			// 视频

			if ("foodVidoe".equals(part.getName())) {

				String head = part.getHeader("content-disposition");

				if (part.getSubmittedFileName()!="") {

					int i = head.lastIndexOf(".");

					String fileext = head.substring(i, head.length()-1);

					String filename = UUID.randomUUID().toString();

				

					video = filename+fileext;

					part.write(filePath+"/"+filename+fileext);

					part.delete();
				}
				else {
					video = request.getParameter("video");
				}
			}

			// 封面图

			if ("foodPICCover".equals(part.getName())) {
				String head = part.getHeader("content-disposition");
				if (part.getSubmittedFileName()!="") {
					int i = head.lastIndexOf(".");
					String fileext = head.substring(i, head.length()-1);
					String filename = UUID.randomUUID().toString();
					pic = filename+fileext;
					part.write(filePath+"/"+filename+fileext);
					part.delete();
				}
				else {
					pic = request.getParameter("CoverPIC");
				}
			}

			// 修改的步骤图

			if ("foodPICStep".equals(part.getName())) {

				int j = 0;

				String head = part.getHeader("content-disposition");

				if (part.getSubmittedFileName()!="") {

					int i = head.lastIndexOf(".");

					String fileext = head.substring(i, head.length()-1);

					String filename = UUID.randomUUID().toString();

					foodPICStep.add(filename+fileext);

					part.write(filePath+"/"+filename+fileext);

					part.delete();

				}

				else {

					foodPICStep.add(StepPIC[j]);

				}

				j++;

			}

			// 新添加的步骤图

			if ("foodPIC".equals(part.getName())) {

				String head = part.getHeader("content-disposition");

				int i = head.lastIndexOf(".");

				String fileext = head.substring(i, head.length()-1);

				String filename = UUID.randomUUID().toString();

				foodPIC.add(filename+fileext);

				part.write(filePath+"/"+filename+fileext);

				part.delete();

			}

		}

		String title = request.getParameter("cook_name"); // 食谱标题

		Integer cookingDifficulty = Integer.parseInt(request.getParameter("cook_difficulty")); // 烹饪难度

		Integer cookingTime = Integer.parseInt(request.getParameter("costtime")); // 烹饪时间

		String taste = request.getParameter("taste"); //口味

		String cookingMethod = request.getParameter("cooking_method"); // 烹饪方式

		String recipeTypeSecondId = request.getParameter("recipeTypeSecond"); // 食谱二级类型id

		String info = request.getParameter("cookStory"); // 简介

		String tips = request.getParameter("cooktips"); // 小贴士

		Cookbook cookbook = new Cookbook();

		cookbook.setTitle(title);

		cookbook.setPic(pic);

		cookbook.setInfo(info);

		cookbook.setTips(tips);

		RecipeTypeSecond recipeTypeSecond = new RecipeTypeSecond();

		recipeTypeSecond.setId(Integer.parseInt(recipeTypeSecondId));

		cookbook.setRecipeTypeSecond(recipeTypeSecond);

		cookbook.setCookingDifficulty(cookingDifficulty);

		cookbook.setCookingTime(cookingTime);

		cookbook.setTaste(taste);

		cookbook.setCookingMethod(cookingMethod);

		cookbook.setVideo(video);

		cookbook.setDraftBox(false);

		cookbook.setId(Integer.parseInt(cookbookId));

		CookbookDAO.getCookbookDAO().updateCookbook(cookbook); // 修改菜谱



		// 修改步骤

		Step step = null;

		String[] setpInfosStep = request.getParameterValues("setpInfosStep"); // 步骤简介

		String[] stepId = request.getParameterValues("stepId");

		for (int i = 0; i < setpInfosStep.length; i++) {

			Cookbook ck = new Cookbook();

			ck.setId(Integer.parseInt(cookbookId));

			step = new Step();

			step.setPic(foodPICStep.get(i));

			step.setInfo(setpInfosStep[i]);

			step.setId(Integer.parseInt(stepId[i]));

			step.setCookbook(ck);

			StepDAO.getStepDAO().updateStep(step); // 修改步骤

		}

		// 新添加步骤

		String[] setpInfos = request.getParameterValues("setpInfos"); // 步骤简介

		if (setpInfos!=null) {

			for (int i = 0; i < setpInfos.length; i++) {

				Cookbook ck = new Cookbook();

				ck.setId(Integer.parseInt(cookbookId));

				step = new Step();

				step.setPic(foodPIC.get(i));

				step.setInfo(setpInfos[i]);

				step.setCookbook(ck);

				StepDAO.getStepDAO().addStepNew(step); // 新添加步骤

			}
			
		}

		

		

		// 修改食材

		String[] zhuliaoIC = request.getParameterValues("zhuliaoIC"); // 食材清单

		String[] zhuliaoValueIC = request.getParameterValues("zhuliaoValueIC"); // 食材用量

		String[] iCId = request.getParameterValues("iCId");

		IngredientsConsumption ingredientsConsumption = null;

		for (int i = 0; i < zhuliaoIC.length; i++) {

			Cookbook ck = new Cookbook();

			ck.setId(Integer.parseInt(cookbookId));

			ingredientsConsumption = new IngredientsConsumption();

			ingredientsConsumption.setIngredientsName(zhuliaoIC[i]);

			ingredientsConsumption.setDosage(zhuliaoValueIC[i]);

			ingredientsConsumption.setId(Integer.parseInt(iCId[i]));

			ingredientsConsumption.setCookbook(ck);

			IngredientsConsumptionDAO.getIngredientsConsumptionDAO().updateIngredients(ingredientsConsumption); // 修改食材

		}

		

		// 新添加的食材

		String[] zhuliao = request.getParameterValues("zhuliao");

		String[] zhuliaoValue = request.getParameterValues("zhuliaoValue");

		if (zhuliao!=null) {

			IngredientsConsumption ingredientsConsumption2 = null;

			for (int i = 0; i < zhuliao.length; i++) {

				Cookbook ck = new Cookbook();

				ck.setId(Integer.parseInt(cookbookId));

				ingredientsConsumption2 = new IngredientsConsumption();

				ingredientsConsumption2.setIngredientsName(zhuliao[i]);

				ingredientsConsumption2.setDosage(zhuliaoValue[i]);

				ingredientsConsumption2.setCookbook(ck);

				IngredientsConsumptionDAO.getIngredientsConsumptionDAO().addIngredientsNew(ingredientsConsumption2);

			}

		}

		
		request.getRequestDispatcher("/CookbookServlet?action=showDraft").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	/**

	 *  通过草稿箱发布草稿

	 * @param request

	 * @param response

	 * @throws ServletException

	 * @throws IOException

	 */

	protected void draftsReleaseDraft(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		Userinfo userinfo = (Userinfo)request.getSession().getAttribute("userinfo");

		//Userinfo userinfo = new Userinfo();

		//userinfo.setId(1);

		String cookbookId = request.getParameter("cookbookId");

		Collection<Part> parts = request.getParts();

		String filePath = request.getServletContext().getRealPath("/static/image/");

		String pic = null; //主图

		String video = null; // 视频

		List<String> foodPICStep = new ArrayList<String>(); // 修改的步骤图

		List<String> foodPIC = new ArrayList<String>();// 新添加的步骤图

		String[] StepPIC = request.getParameterValues("StepPIC");

		for (Part part : parts) {

			// 视频

			if ("foodVidoe".equals(part.getName())) {

				String head = part.getHeader("content-disposition");

				if (part.getSubmittedFileName()!="") {

					int i = head.lastIndexOf(".");

					String fileext = head.substring(i, head.length()-1);

					String filename = UUID.randomUUID().toString();

					video = filename+fileext;

					part.write(filePath+"/"+filename+fileext);

					part.delete();

				}

				else {

					System.out.println(request.getParameter("video"));

					video = request.getParameter("video");

				}

				

			}

			// 封面图

			if ("foodPICCover".equals(part.getName())) {

				String head = part.getHeader("content-disposition");

				if (part.getSubmittedFileName()!="") {

					int i = head.lastIndexOf(".");

					String fileext = head.substring(i, head.length()-1);

					String filename = UUID.randomUUID().toString();

					pic = filename+fileext;

					part.write(filePath+"/"+filename+fileext);

					part.delete();

				}

				else {

					pic = request.getParameter("CoverPIC");

				}

				

			}

			// 修改的步骤图

			if ("foodPICStep".equals(part.getName())) {

				int j = 0;

				String head = part.getHeader("content-disposition");

				if (part.getSubmittedFileName()!="") {

					int i = head.lastIndexOf(".");

					String fileext = head.substring(i, head.length()-1);

					String filename = UUID.randomUUID().toString();

					foodPICStep.add(filename+fileext);

					part.write(filePath+"/"+filename+fileext);

					part.delete();

				}

				else {

					foodPICStep.add(StepPIC[j]);

				}

				j++;

			}

			// 新添加的步骤图

			if ("foodPIC".equals(part.getName())) {

				String head = part.getHeader("content-disposition");

				int i = head.lastIndexOf(".");

				String fileext = head.substring(i, head.length()-1);

				String filename = UUID.randomUUID().toString();

				foodPIC.add(filename+fileext);

				part.write(filePath+"/"+filename+fileext);

				part.delete();

			}

		}

		String title = request.getParameter("cook_name"); // 食谱标题

		Integer cookingDifficulty = Integer.parseInt(request.getParameter("cook_difficulty")); // 烹饪难度

		Integer cookingTime = Integer.parseInt(request.getParameter("costtime")); // 烹饪时间

		String taste = request.getParameter("taste"); //口味

		String cookingMethod = request.getParameter("cooking_method"); // 烹饪方式

		String recipeTypeSecondId = request.getParameter("recipeTypeSecond"); // 食谱二级类型id

		String info = request.getParameter("cookStory"); // 简介

		String tips = request.getParameter("cooktips"); // 小贴士

		Cookbook cookbook = new Cookbook();

		cookbook.setTitle(title);

		cookbook.setPic(pic);

		cookbook.setInfo(info);

		cookbook.setTips(tips);

		RecipeTypeSecond recipeTypeSecond = new RecipeTypeSecond();

		recipeTypeSecond.setId(Integer.parseInt(recipeTypeSecondId));

		cookbook.setRecipeTypeSecond(recipeTypeSecond);

		cookbook.setCookingDifficulty(cookingDifficulty);

		cookbook.setCookingTime(cookingTime);

		cookbook.setTaste(taste);

		cookbook.setCookingMethod(cookingMethod);

		cookbook.setVideo(video);

		cookbook.setDraftBox(true);

		cookbook.setId(Integer.parseInt(cookbookId));

		CookbookDAO.getCookbookDAO().updateCookbook(cookbook); // 修改菜谱



		// 修改步骤

		Step step = null;

		String[] setpInfosStep = request.getParameterValues("setpInfosStep"); // 步骤简介

		String[] stepId = request.getParameterValues("stepId");

		for (int i = 0; i < setpInfosStep.length; i++) {

			Cookbook ck = new Cookbook();

			ck.setId(Integer.parseInt(cookbookId));

			step = new Step();

			step.setPic(foodPICStep.get(i));

			step.setInfo(setpInfosStep[i]);

			step.setId(Integer.parseInt(stepId[i]));

			step.setCookbook(ck);

			StepDAO.getStepDAO().updateStep(step); // 修改步骤

		}

		// 新添加步骤

		String[] setpInfos = request.getParameterValues("setpInfos"); // 步骤简介

		if (setpInfos!=null) {

			for (int i = 0; i < setpInfos.length; i++) {

				Cookbook ck = new Cookbook();

				ck.setId(Integer.parseInt(cookbookId));

				step = new Step();

				step.setPic(foodPIC.get(i));

				step.setInfo(setpInfos[i]);

				step.setCookbook(ck);

				StepDAO.getStepDAO().addStepNew(step); // 新添加步骤

			}

		}
		// 修改食材

		String[] zhuliaoIC = request.getParameterValues("zhuliaoIC"); // 食材清单

		String[] zhuliaoValueIC = request.getParameterValues("zhuliaoValueIC"); // 食材用量

		String[] iCId = request.getParameterValues("iCId");

		IngredientsConsumption ingredientsConsumption = null;

		for (int i = 0; i < zhuliaoIC.length; i++) {

			Cookbook ck = new Cookbook();

			ck.setId(Integer.parseInt(cookbookId));

			ingredientsConsumption = new IngredientsConsumption();

			ingredientsConsumption.setIngredientsName(zhuliaoIC[i]);

			ingredientsConsumption.setDosage(zhuliaoValueIC[i]);

			ingredientsConsumption.setId(Integer.parseInt(iCId[i]));

			ingredientsConsumption.setCookbook(ck);

			IngredientsConsumptionDAO.getIngredientsConsumptionDAO().updateIngredients(ingredientsConsumption); // 修改食材

		}

		

		// 新添加的食材

		String[] zhuliao = request.getParameterValues("zhuliao");

		String[] zhuliaoValue = request.getParameterValues("zhuliaoValue");

		if (zhuliao!=null) {

			IngredientsConsumption ingredientsConsumption2 = null;

			for (int i = 0; i < zhuliao.length; i++) {

				Cookbook ck = new Cookbook();

				ck.setId(Integer.parseInt(cookbookId));

				ingredientsConsumption2 = new IngredientsConsumption();

				ingredientsConsumption2.setIngredientsName(zhuliao[i]);

				ingredientsConsumption2.setDosage(zhuliaoValue[i]);

				ingredientsConsumption2.setCookbook(ck);

				IngredientsConsumptionDAO.getIngredientsConsumptionDAO().addIngredientsNew(ingredientsConsumption2);

			}

		}
		response.sendRedirect(request.getContextPath());
	}
	
	  // 展示食谱

    protected void cookbookShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String cookbook_id = request.getParameter("id");
      // 获得了食谱 和 作者
      Cookbook cookbook = CookbookDAO.getCookbookDAO().findCookbookByCookbookId(Integer.parseInt(cookbook_id));
      // 获得食材
      List<IngredientsConsumption> ingredientsConsumptions = IngredientsConsumptionDAO.getIngredientsConsumptionDAO().findConsumptionByCookbookId(Integer.parseInt(cookbook_id));
      // 获得步骤
      List<Step> steps = StepDAO.getStepDAO().findStepsByCookbookId(Integer.parseInt(cookbook_id));
      // 获得评论
      
      ArrayList<CookbookComment> firstComments = CookbookCommentDAO.getCookbookCommentDAO().getFirstComments(Integer.parseInt(cookbook_id));
      request.setAttribute("firstComments", firstComments);
      // 收藏量
      int collectCount = CookbookCollectDAO.getCollectCookbookCollectDAO().findCookbookCollectCountByCookbookId(Integer.parseInt(cookbook_id));
      
      request.setAttribute("collectCount", collectCount);
      request.setAttribute("cookbook", cookbook);
      request.setAttribute("ingredientsConsumptions", ingredientsConsumptions);
      request.setAttribute("steps", steps);
      request.getRequestDispatcher("/cookbookShow.jsp").forward(request, response);
    }

	/*
	 * protected void cookbookShow(HttpServletRequest request, HttpServletResponse
	 * response) throws ServletException, IOException { String cookbook_id =
	 * request.getParameter("id"); // 获得了食谱 和 作者 Cookbook cookbook =
	 * CookbookDAO.getCookbookDAO().findCookbookByCookbookId(Integer.parseInt(
	 * cookbook_id)); // 获得食材 List<IngredientsConsumption> ingredientsConsumptions =
	 * IngredientsConsumptionDAO.getIngredientsConsumptionDAO().
	 * findConsumptionByCookbookId(Integer.parseInt(cookbook_id)); // 获得步骤
	 * List<Step> steps =
	 * StepDAO.getStepDAO().findStepsByCookbookId(Integer.parseInt(cookbook_id)); //
	 * 获得评论 ArrayList<CookbookComment> firstComments =
	 * CookbookCommentDAO.getCookbookCommentDAO().getFirstComments(Integer.parseInt(
	 * cookbook_id)); request.setAttribute("firstComments", firstComments);
	 * 
	 * int CollectCount = CookbookCollectDAO.getCollectCookbookCollectDAO().
	 * findCookbookCollectCountByCookbookId(Integer.parseInt(cookbook_id));
	 * 
	 * request.setAttribute("CollectCount", CollectCount);
	 * request.setAttribute("cookbook", cookbook);
	 * request.setAttribute("ingredientsConsumptions", ingredientsConsumptions);
	 * request.setAttribute("steps", steps);
	 * request.getRequestDispatcher("/cookbookShow.jsp").forward(request, response);
	 * }
	 */
	
	protected void upCookbook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//Userinfo userinfo = (Userinfo)request.getSession().getAttribute("userinfo");
		Collection<Part> parts = request.getParts();
		String filePath = request.getServletContext().getRealPath("/static/image/");
		String pic = null; //涓诲浘
		String video = null; // 瑙嗛
		List<String> foodPICs = new ArrayList<String>();
		for (Part part : parts) {
			// 瑙嗛
			if ("foodVidoe".equals(part.getName())) {
				String head = part.getHeader("content-disposition");
				int i = head.lastIndexOf(".");
				String fileext = head.substring(i, head.length()-1);
				String filename = UUID.randomUUID().toString();
			;
				video = filename+fileext;
				part.write(filePath+"/"+filename+fileext);
				part.delete();
			}
			// 灏侀潰鍥�
			if ("foodPICCover".equals(part.getName())) {
				String head = part.getHeader("content-disposition");
				int i = head.lastIndexOf(".");
				String fileext = head.substring(i, head.length()-1);
				String filename = UUID.randomUUID().toString();
				pic = filename+fileext;
				part.write(filePath+"/"+filename+fileext);
				part.delete();
			}
			// 姝ラ鍥�
			if ("foodPIC".equals(part.getName())) {
				String head = part.getHeader("content-disposition");
				int i = head.lastIndexOf(".");
				String fileext = head.substring(i, head.length()-1);
				String filename = UUID.randomUUID().toString();
				foodPICs.add(filename+fileext);
				part.write(filePath+"/"+filename+fileext);
				part.delete();
			}
		}
		Userinfo userinfo = new Userinfo();
		userinfo.setId(1);
		String title = request.getParameter("cook_name"); // 椋熻氨鏍囬
		Integer cookingDifficulty = Integer.parseInt(request.getParameter("cook_difficulty")); // 鐑归オ闅惧害
		Integer cookingTime = Integer.parseInt(request.getParameter("costtime")); // 鐑归オ鏃堕棿
		String taste = request.getParameter("taste"); //鍙ｅ懗
		String cookingMethod = request.getParameter("cooking_method"); // 鐑归オ鏂瑰紡
		String recipeTypeSecondId = request.getParameter("recipeTypeSecond"); // 椋熻氨浜岀骇绫诲瀷id
		String info = request.getParameter("cookStory"); // 绠�浠�
		String tips = request.getParameter("cooktips"); // 灏忚创澹�
		Cookbook cookbook = new Cookbook();
		cookbook.setTitle(title);
		cookbook.setPic(pic);
		cookbook.setUserinfo(userinfo);
		cookbook.setInfo(info);
		cookbook.setTips(tips);
		RecipeTypeSecond recipeTypeSecond = new RecipeTypeSecond();
		recipeTypeSecond.setId(Integer.parseInt(recipeTypeSecondId));
		cookbook.setRecipeTypeSecond(recipeTypeSecond);
		cookbook.setCookingDifficulty(cookingDifficulty);
		cookbook.setCookingTime(cookingTime);
		cookbook.setTaste(taste);
		cookbook.setCookingMethod(cookingMethod);
		cookbook.setVideo(video);
		cookbook.setDraftBox(false);
		CookbookDAO.getCookbookDAO().addCookbook(cookbook); // 涓婁紶鑿滆氨
		
//		Integer lastId = CookbookDAO.getCookbookDAO().lastId();  // 鑾峰緱id
//		System.out.println(lastId);

		// 娣诲姞姝ラ
		Step step = null;
		String[] setpInfos = request.getParameterValues("setpInfos"); // 姝ラ绠�浠�
		for (int i = 0; i < setpInfos.length; i++) {
			step = new Step();
			step.setPic(foodPICs.get(i));
			step.setInfo(setpInfos[i]);
			StepDAO.getStepDAO().addStep(step); // 娣诲姞姝ラ
		}
		
		// 娣诲姞椋熸潗
		String[] zhuliao = request.getParameterValues("zhuliao"); // 椋熸潗娓呭崟
		String[] zhuliaoValue = request.getParameterValues("zhuliaoValue"); // 椋熸潗鐢ㄩ噺
		IngredientsConsumption ingredientsConsumption = null;
		for (int i = 0; i < zhuliao.length; i++) {
			ingredientsConsumption = new IngredientsConsumption();
			ingredientsConsumption.setIngredientsName(zhuliao[i]);
			ingredientsConsumption.setDosage(zhuliaoValue[i]);
			IngredientsConsumptionDAO.getIngredientsConsumptionDAO().addIngredients(ingredientsConsumption); // 娣诲姞椋熸潗
		}
		response.sendRedirect(request.getContextPath());
	}
	// 鍏堣幏寰椾竴绾ц彍璋� 杞彂鍘讳笂浼犺彍璋遍〉闈�
	protected void init(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(request.getSession().getAttribute("userinfo")==null) {
			request.getRequestDispatcher("/login.jsp").forward(request, response);
		}
		else {
			List<RecipeType> findByRecipeType = RecipeTypeDAO.getRecipeTypeDAO().findByRecipeType();
			request.setAttribute("findByRecipeType", findByRecipeType);
			request.getRequestDispatcher("/upCookbook.jsp").forward(request, response);
		}
	}

}
